<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class BookList extends Model implements Sortable
{
    use SortableTrait;

    public $sortable = [
        'order_column_name' => 'order',
        'sort_when_creating' => true,
    ];

    protected $fillable = ['title','intro','list_image','detail_image','system_collect_num','order'];

    public function category()
    {
        return $this->belongsTo(BookListCategory::class,'category_id');
    }

    public function books()
    {
        return $this->belongsToMany(Book::class,'book_list_book_relations','book_list_id')->orderBy('sort','asc');
    }

    public function user_collects()
    {
        return $this->belongsToMany(User::class,'book_list_user_collect_relations','book_list_id')->orderBy('user_type','desc')->orderBy('book_list_user_collect_relations.id','desc');
    }
}
